package bootstrap

import (
	"context"
	"fmt"
	"go-mp-starter/global"

	"github.com/go-redis/redis/v8"
	"go.uber.org/zap"
)

// InitRedis 初始化Redis驱动
func InitRedis() (client *redis.Client) {

	var config = global.App.Config.Redis

	client = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", config.Host, config.Port),
		DB:       config.DB,
		Password: config.Password,
	})
	// ping Redis client test connection
	_, err := client.Ping(context.Background()).Result()
	if err != nil {
		global.App.Log.Error("Redis connect ping fail: ", zap.Any("err", err))
		return nil
	}
	return
}
